//
//  HMEmoticonViewCell.swift
//  Weibo
//
//  Created by fuyu on 2017/8/4.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit

class HMEmoticonViewCell: UICollectionViewCell {
    
    //  cell对应的数据源
    var emoticonArray: [HMEmoticon]? {
        didSet {
            
            
            guard let currentEmoticonArray = emoticonArray else {
                return
            }
            
            
            //  默认隐藏所有表情按钮
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            
            //  遍历表情模型数组
            for (i, emoticon) in currentEmoticonArray.enumerated() {
                //  获取表情按钮
                let button = emoticonButtonArray[i]
                //  给按钮绑定数据显示按钮
                button.isHidden = false
                if emoticon.type == "0" {
                    //  图片表情
                    //  不能直接加载图片，因为图片在EmoitconBundle里面
                    //                    let image = UIImage(named: emoticon.png!)
                    //                    let path = HMEmoticonTools.sharedTools.emoticonBundle.resourcePath
                    //                    print(path)
                    //  在指定bundle里面加载图片
                    let image = UIImage(named: emoticon.path!, in: HMEmoticonTools.sharedTools.emoticonBundle, compatibleWith: nil)
                    button.setImage(image, for: .normal)
                    button.setTitle(nil, for: .normal)
                } else {
                    //  通过oc分类转成emoji表情
                    let emoji = (emoticon.code! as NSString).emoji()
                    button.setTitle(emoji, for: .normal)
                    button.setImage(nil, for: .normal)
                }
                
            }
            
        }
    }
    //  记录20个表情按钮
    private lazy var emoticonButtonArray: [UIButton] = [UIButton]()
    //  MARK: --    懒加载
    private lazy var messageLabel: UILabel = {
        let label = UILabel(fontSize: 20, textColor: UIColor.blue)
        return label
    }()
    
    var indexPath: IndexPath? {
        didSet {
            guard let currentIndexPath = indexPath else {
                return
            }
            
            messageLabel.text = "当前滚动的是第\(currentIndexPath.section + 1)组第\(currentIndexPath.item + 1)页"
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(messageLabel)
        addChildButton()
        messageLabel.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
    }
    //  添加20个表情按钮
    private func addChildButton() {
        //  每项的宽度
        let buttonWidth = width / 7
        //  每项的高度
        let buttonHeight = height / 3
        for i in 0..<20 {
            //  计算列索引
            let colIndex = i % 7
            //  计算行的索引
            let rowIndex = i / 7
            
            let button = UIButton()
            //  设置x，y
            button.x = CGFloat(colIndex) * buttonWidth
            button.y = CGFloat(rowIndex) * buttonHeight
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            //  设置文字大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            contentView.addSubview(button)
            //  记录表情按钮
            emoticonButtonArray.append(button)
        }
    }
    
}
